package sun.superstring;

public class ArrayTest4 {
    public static void main(String[] args) {
        // 目标：二维数组的认识

        String [][] classmates = new String [][] {
                {"张无忌", "赵敏","周芷若"},
                {"张三丰", "宋远桥", "殷梨亭"},
                {"灭绝师太", "陈坤","玄冥二老","金毛狮王"},
                {"杨逍", "纪晓芙"}
        };
        printArray(classmates);

        printArray2(classmates);
        System.out.println("======================================");
        printArray3(classmates);
        printArray2(classmates);
    }

    public static void printArray(String [][] classmates) {

        // 动态初始化数组
        // int [][] arr = new int [3][5];

        // 访问1：数组名[行索引]
        String [] names = classmates[2];
        for(int i = 0; i < names.length; i++){
            System.out.println(names[i]);
        }

        // 访问2：数组名[行索引][列索引]
        System.out.println(classmates[1][1]);
        System.out.println(classmates[2][2]);

        // 长度的访问：数组名.length
        System.out.println(classmates.length); // 4 =====> 行数
        System.out.println(classmates[0].length); // 3 =====> 0行中的列数
    }

    // 遍历二维数组
    public static void printArray2(String [][] classmates) {

        for(int i = 0; i < classmates.length; i++){
            for(int j = 0; j < classmates[i].length; j++){
                System.out.print(classmates[i][j] + "\t");
            }
            System.out.println();
        }

    }

    // 随机排座位
    public static void printArray3(String [][] classmates) {
        for(int i = 0; i < classmates.length; i++){
            for(int j = 0; j < classmates[i].length; j++){

                int indexRow = (int)(Math.random() * classmates.length);
                int indexCol = 0;
                switch (indexRow){
                    case 0:
                        indexCol = (int)(Math.random() * classmates[0].length);
                        break;
                    case 1:
                        indexCol = (int)(Math.random() * classmates[1].length);
                        break;
                    case 2:
                        indexCol = (int)(Math.random() * classmates[2].length);
                        break;
                    case 3:
                        indexCol = (int)(Math.random() * classmates[3].length);
                }

                String temp = classmates[i][j];
                classmates[i][j] = classmates[indexRow][indexCol];
                classmates[indexRow][indexCol] = temp;

            }
        }
    }


}
